<?php
/**
 *   Joomdiet - administrator/components/com_joomdiet/controller/dietmanager.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2011 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
jimport('joomla.filesystem.file');

class JoomdietControllerDietmanager extends JController
{
    function __construct() {
        // get requested view
        if(JRequest::getCmd('view') == '') {
            JRequest::setVar('view', 'dietmanager');
        }
        $this->item_type = 'Default';
        parent::__construct();
        
        $this->registerTask('addDiet', 'editDiet');
    }
    
    function editDiet()
    {
        JRequest::setVar('view', 'editFood');
        JRequest::setVar('hidemainmenu', 1);
        
        $view =& $this->getView('editdietmanager', 'html');
        $view->setModel($this->getModel('Dietmanager'), true);
        
        $view->display();
    }
    
    function removeDiet()
    {
        $model = $this->getModel('dietmanager');
        $cid = JRequest::getVar('cid');
        foreach($cid as $id) {
            $model->setId($id);
            $model->remove();
        }
        
        $link = 'index.php?option=com_joomdiet&view=dietmanager';
        $this->setRedirect($link, JText::_('COM_JOOMDIET_DELETE_SUCCESS'));
    }
    
    function save() {
        $this->apply();
        
        $link = 'index.php?option=com_joomdiet&view=dietmanager';
        $this->setRedirect($link, JText::_('COM_JOOMDIET_SAVE_DIET'));
    }
    
    function apply() {
        $model = $this->getModel('dietmanager');
        
        if (!$model->store()) {
            $link = 'index.php?option=com_joomdiet&view=dietmanager';
            $this->setRedirect($link, JText::_('COM_JOOMDIET_SAVE_DIET_ERROR'));
        }
    }
    
    
    function download() {
        $model = $this->getModel('dietmanager');
        
        if ($file = $model->file_to_read()) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.$file->file_name);
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: '.$file->file_size);
            
            ob_clean();
            flush();
            readfile($file->file_url);
            exit;
        }
    }
}
?>
